package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.contractsurcharge.pojo.dto.ContractSurchargeDeteleBatchDTO;
import com.xbongbong.pro.contractsurcharge.pojo.vo.ContractSurchargeDeteleBatchVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.domain.entity.ContractSurchargeEntity;
import com.xbongbong.saas.model.ContractSurchargeModel;
import com.xbongbong.saas.service.ContractSurchargeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 合同的其他费用
 * @author 吴峰
 * @version v1.0
 * @date 2019/3/13 19:04
 * @since v1.0
 */
@Service("contractSurchargeService")
public class ContractSurchargeServiceImpl implements ContractSurchargeService {

    private static final Logger LOG = LoggerFactory.getLogger(ContractSurchargeServiceImpl.class);

    @Resource
    private ContractSurchargeModel contractSurchargeModel;

    @Override
    public ContractSurchargeDeteleBatchVO deleteBatchByContractId(ContractSurchargeDeteleBatchDTO contractSurchargeDeteleBatchDTO) throws XbbException {
        ContractSurchargeDeteleBatchVO contractSurchargeDeteleBatchVO = new ContractSurchargeDeteleBatchVO();
        try {
            List<Long> contractIdIn = contractSurchargeDeteleBatchDTO.getContractIdIn();
            String corpid = contractSurchargeDeteleBatchDTO.getCorpid();
            Map<String, Object> param = BeanUtil.convertBean2Map(contractSurchargeDeteleBatchDTO, true);
            param.put("contractIdIn", contractIdIn);
            List<ContractSurchargeEntity> contractSurchargeList = contractSurchargeModel.findEntitys(param);
            if (Objects.isNull(contractSurchargeList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            if (!contractSurchargeList.isEmpty()) {
                List<Long> contractSurchargeIdList = new ArrayList<>();
                contractSurchargeList.forEach((item)->{
                    contractSurchargeIdList.add(item.getId());
                });
                contractSurchargeModel.deleteBatch(contractSurchargeIdList, corpid);
            }
        } catch (Exception e) {
            LOG.error("ContractSurchargeServiceImpl.deleteBatchByContractId 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return contractSurchargeDeteleBatchVO;
    }
}
